package io.renren.modules.sys.controller;


import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.service.entity.ReportIPQCEntity;
import io.renren.modules.service.service.ReportIPQCService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pangzhihua
 * @date 2019-06-17 14:19:50
 */

@RestController
@RequestMapping("sys/report")
public class SysReportController {


    @Autowired
    public ReportIPQCService reportIPQCService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = reportIPQCService.queryReportIPQCAll(params);
        return R.ok().put("page", page);
    }


    @RequestMapping("/ipqcReport")
    public R ipqcReport(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("id")) {
            return R.error(20310, "参数错误！");
        }
        int id = Integer.parseInt(params.get("id").toString());
        ReportIPQCEntity reportIPQCEntity = reportIPQCService.getIpqcReport(id);
        Map data = new HashMap<String, Object>();
        data.put("reportIPQC", reportIPQCEntity);
        return R.ok(data);
    }

    /**
     * 保存巡查报告
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/saveIpqcReport", method = RequestMethod.POST)
    public R saveIpqcReport(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("model") || !params.containsKey("content")) {
            return R.error(20310, "参数错误！");
        }
        String model = params.get("model").toString();
        String content = params.get("content").toString();
        if (!isJSONValid(content)) {
            return R.error(20312, "参数格式错误！");
        }
        ReportIPQCEntity reportIPQCEntity = new ReportIPQCEntity();
        reportIPQCEntity.setRiModel(model);
        reportIPQCEntity.setRiContent(content);
        reportIPQCService.saveOrUpdateIpqcReport(reportIPQCEntity);
        return R.ok();
    }


    @PostMapping("/updateIpqcReport")
    public R updateIpqcReport(@RequestBody ReportIPQCEntity reportIPQCEntity) {
        if (reportIPQCEntity.getRiId() == null || reportIPQCEntity.getRiContent() == null) {
            return R.error(20310, "参数错误！");
        }

        if (!isJSONValid(reportIPQCEntity.getRiContent())) {
            return R.error(20312, "参数格式错误！");
        }

        reportIPQCService.saveOrUpdateIpqcReport(reportIPQCEntity);
        return R.ok();
    }

    @PostMapping("/delete")
    @RequiresPermissions("sys:report:delete")
    public R deleteIpqcReport(@RequestBody Integer[] ids) {
        if (ids.length <= 0) {
            return R.error(20310, "参数错误！");
        }

        List<ReportIPQCEntity> reportIPQCEntityList = new ArrayList<>();
        for (int id : ids) {
            ReportIPQCEntity reportIPQCEntity = new ReportIPQCEntity();
            reportIPQCEntity.setRiId(id);
            reportIPQCEntity.setRiStatus(-1);

            reportIPQCEntityList.add(reportIPQCEntity);
        }

        reportIPQCService.updateIpqcReport(reportIPQCEntityList);

        return R.ok();
    }


    /**
     * 判断json格式是否正确
     *
     * @param test
     * @return
     */
    public boolean isJSONValid(String test) {
        try {
            JSONObject.parseObject(test);
        } catch (JSONException ex) {
            try {
                JSONObject.parseArray(test);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }
}
